package org.htmlparser;

import java.io.Serializable;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import org.htmlparser.lexer.Page;
import org.htmlparser.nodes.RemarkNode;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.AppletTag;
import org.htmlparser.tags.BaseHrefTag;
import org.htmlparser.tags.BodyTag;
import org.htmlparser.tags.Bullet;
import org.htmlparser.tags.BulletList;
import org.htmlparser.tags.DefinitionList;
import org.htmlparser.tags.DefinitionListBullet;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.DoctypeTag;
import org.htmlparser.tags.FormTag;
import org.htmlparser.tags.FrameSetTag;
import org.htmlparser.tags.FrameTag;
import org.htmlparser.tags.HeadTag;
import org.htmlparser.tags.HeadingTag;
import org.htmlparser.tags.Html;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.InputTag;
import org.htmlparser.tags.JspTag;
import org.htmlparser.tags.LabelTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.MetaTag;
import org.htmlparser.tags.ObjectTag;
import org.htmlparser.tags.OptionTag;
import org.htmlparser.tags.ParagraphTag;
import org.htmlparser.tags.ProcessingInstructionTag;
import org.htmlparser.tags.ScriptTag;
import org.htmlparser.tags.SelectTag;
import org.htmlparser.tags.Span;
import org.htmlparser.tags.StyleTag;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableHeader;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.tags.TextareaTag;
import org.htmlparser.tags.TitleTag;

public class PrototypicalNodeFactory
  implements Serializable, NodeFactory
{
  protected Map mBlastocyst;
  protected Remark mRemark;
  protected Tag mTag;
  protected Text mText;

  public PrototypicalNodeFactory()
  {
    this(false);
  }

  public PrototypicalNodeFactory(Tag paramTag)
  {
    this(true);
    registerTag(paramTag);
  }

  public PrototypicalNodeFactory(boolean paramBoolean)
  {
    clear();
    this.mText = new TextNode(null, 0, 0);
    this.mRemark = new RemarkNode(null, 0, 0);
    this.mTag = new TagNode(null, 0, 0, null);
    if (!(paramBoolean))
      registerTags();
  }

  public PrototypicalNodeFactory(Tag[] paramArrayOfTag)
  {
    this(true);
    for (int i = 0; i < paramArrayOfTag.length; ++i)
      registerTag(paramArrayOfTag[i]);
  }

  public void clear()
  {
    this.mBlastocyst = new Hashtable();
  }

  public Remark createRemarkNode(Page paramPage, int paramInt1, int paramInt2)
  {
    Object localObject;
    try
    {
      localObject = (Remark)(Remark)getRemarkPrototype().clone();
      ((Remark)localObject).setPage(paramPage);
      ((Remark)localObject).setStartPosition(paramInt1);
      ((Remark)localObject).setEndPosition(paramInt2);
      return localObject;
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      localObject = new RemarkNode(paramPage, paramInt1, paramInt2);
    }
  }

  public Text createStringNode(Page paramPage, int paramInt1, int paramInt2)
  {
    Object localObject;
    try
    {
      localObject = (Text)(Text)getTextPrototype().clone();
      ((Text)localObject).setPage(paramPage);
      ((Text)localObject).setStartPosition(paramInt1);
      ((Text)localObject).setEndPosition(paramInt2);
      return localObject;
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      localObject = new TextNode(paramPage, paramInt1, paramInt2);
    }
  }

  public Tag createTagNode(Page paramPage, int paramInt1, int paramInt2, Vector paramVector)
  {
    String str1;
    Object localObject = null;
    if (paramVector.size() != 0)
    {
      str1 = ((Attribute)paramVector.elementAt(0)).getName();
      if (str1 != null);
    }
    try
    {
      String str2 = str1.toUpperCase(Locale.ENGLISH);
      if (!(str2.startsWith("/")))
      {
        if (str2.endsWith("/"))
          str2 = str2.substring(0, str2.length() - 1);
        Tag localTag = (Tag)this.mBlastocyst.get(str2);
        if (localTag != null)
        {
          localObject = (Tag)localTag.clone();
          ((Tag)localObject).setPage(paramPage);
          ((Tag)localObject).setStartPosition(paramInt1);
          ((Tag)localObject).setEndPosition(paramInt2);
          ((Tag)localObject).setAttributesEx(paramVector);
        }
      }
      if (localObject == null);
    }
    catch (CloneNotSupportedException localCloneNotSupportedException2)
    {
      try
      {
        localObject = (Tag)getTagPrototype().clone();
        ((Tag)localObject).setPage(paramPage);
        ((Tag)localObject).setStartPosition(paramInt1);
        ((Tag)localObject).setEndPosition(paramInt2);
        ((Tag)localObject).setAttributesEx(paramVector);
        return localObject;
      }
      catch (CloneNotSupportedException localCloneNotSupportedException1)
      {
        while (true)
          localObject = new TagNode(paramPage, paramInt1, paramInt2, paramVector);
        localCloneNotSupportedException2 = localCloneNotSupportedException2;
      }
    }
  }

  public Tag get(String paramString)
  {
    return ((Tag)this.mBlastocyst.get(paramString));
  }

  public Remark getRemarkPrototype()
  {
    return this.mRemark;
  }

  public Set getTagNames()
  {
    return this.mBlastocyst.keySet();
  }

  public Tag getTagPrototype()
  {
    return this.mTag;
  }

  public Text getTextPrototype()
  {
    return this.mText;
  }

  public Tag put(String paramString, Tag paramTag)
  {
    return ((Tag)this.mBlastocyst.put(paramString, paramTag));
  }

  public void registerTag(Tag paramTag)
  {
    String[] arrayOfString = paramTag.getIds();
    for (int i = 0; i < arrayOfString.length; ++i)
      put(arrayOfString[i].toUpperCase(Locale.ENGLISH), paramTag);
  }

  public PrototypicalNodeFactory registerTags()
  {
    registerTag(new AppletTag());
    registerTag(new BaseHrefTag());
    registerTag(new Bullet());
    registerTag(new BulletList());
    registerTag(new DefinitionList());
    registerTag(new DefinitionListBullet());
    registerTag(new DoctypeTag());
    registerTag(new FormTag());
    registerTag(new FrameSetTag());
    registerTag(new FrameTag());
    registerTag(new HeadingTag());
    registerTag(new ImageTag());
    registerTag(new InputTag());
    registerTag(new JspTag());
    registerTag(new LabelTag());
    registerTag(new LinkTag());
    registerTag(new MetaTag());
    registerTag(new ObjectTag());
    registerTag(new OptionTag());
    registerTag(new ParagraphTag());
    registerTag(new ProcessingInstructionTag());
    registerTag(new ScriptTag());
    registerTag(new SelectTag());
    registerTag(new StyleTag());
    registerTag(new TableColumn());
    registerTag(new TableHeader());
    registerTag(new TableRow());
    registerTag(new TableTag());
    registerTag(new TextareaTag());
    registerTag(new TitleTag());
    registerTag(new Div());
    registerTag(new Span());
    registerTag(new BodyTag());
    registerTag(new HeadTag());
    registerTag(new Html());
    return this;
  }

  public Tag remove(String paramString)
  {
    return ((Tag)this.mBlastocyst.remove(paramString));
  }

  public void setRemarkPrototype(Remark paramRemark)
  {
    if (paramRemark == null)
      this.mRemark = new RemarkNode(null, 0, 0);
    while (true)
    {
      return;
      this.mRemark = paramRemark;
    }
  }

  public void setTagPrototype(Tag paramTag)
  {
    if (paramTag == null)
      this.mTag = new TagNode(null, 0, 0, null);
    while (true)
    {
      return;
      this.mTag = paramTag;
    }
  }

  public void setTextPrototype(Text paramText)
  {
    if (paramText == null)
      this.mText = new TextNode(null, 0, 0);
    while (true)
    {
      return;
      this.mText = paramText;
    }
  }

  public void unregisterTag(Tag paramTag)
  {
    String[] arrayOfString = paramTag.getIds();
    for (int i = 0; i < arrayOfString.length; ++i)
      remove(arrayOfString[i].toUpperCase(Locale.ENGLISH));
  }
}